package com.pg.agent.engine.client.model.llm;


import com.pg.agent.engine.client.model.ComponentInParam;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 大模型调用信息类
 *
 * @author huhuaping
 * @date 2023/11/18 10:21
 */
@Data
public class CallLog {

    /**
     * 系统提示词
     */
    private String systemPrompt;
    /**
     * 用户提示词
     */
    private String userPrompt;
    /**
     * 模型响应
     */
    private String response;
    /**
     * 图片路径集合
     */
    private List<String> imageList = new ArrayList<>();

    private List<String> labelList = new ArrayList<>();
    /**
     * 模型
     */
    private String model;
    /**
     * 模型版本
     */
    private String modelVersion;
    /**
     * 调用时长
     */
    private long timeLong;
    /**
     * 调用对象
     */
    private String callName;
    /**
     * 调用类型
     */
    private String callType;
    /**
     * 开始调用时长
     */
    private Date callStartTime;
    /**
     * 调用结束时间
     */
    private Date callEndTime;
    /**
     * sopID
     */
    private long sopId;
    /**
     * stateId
     */
    private long stateId;
    /**
     * agentId
     */
    private long agentId;

    private String arguments;
    /**
     * agent 所有组件需要用户输入的入参
     */
    private List<ComponentInParam> componentUserInPutParam;

    private String agentResponse;


    public CallLog() {

        this.callStartTime = new Date();
    }


    public CallLog(String callName, String callType) {
        this.callStartTime = new Date();
        this.callName = callName;
        this.callType = callType;
    }

    public CallLog(Long sopId, String callName, String callType) {
        this.callStartTime = new Date();
        this.sopId = sopId;
        this.callName = callName;
        this.callType = callType;
    }

    public CallLog(Long sopId, Long agentId, String callName, String callType) {
        this.callStartTime = new Date();
        this.agentId = agentId;
        this.sopId = sopId;
        this.callName = callName;
        this.callType = callType;
    }

    public void setLLmPromptInfo(List<LLMPrompt> promptList,ComponentLLM llm, String response) {

        setLlm(llm);

        StringBuilder sysPromptSb = new StringBuilder("");
        StringBuilder promptSb = new StringBuilder("");
        for (LLMPrompt llmPrompt : promptList) {
            if (LLMPromptType.SYSTEM.getCode().equals(llmPrompt.getRole())) {
                sysPromptSb.append("角色：system；内容：" + StringUtils.trimToEmpty(llmPrompt.getContent()));
            }else {
                promptSb.append("角色：user；内容：" + StringUtils.trimToEmpty(llmPrompt.getContent())+"\n");
            }
        }
        this.systemPrompt = sysPromptSb.toString();
        this.userPrompt = promptSb.toString();
        this.response = response;
        this.setCallEndTime(new Date());
        this.setTimeLong(System.currentTimeMillis() - this.callStartTime.getTime());
    }

    public void setTimeLong(){
        this.setTimeLong(System.currentTimeMillis() - this.callStartTime.getTime());
    }

    public void setLlm(ComponentLLM llm) {
        this.model = llm.getModel() + "(" + (llm.getType() != null && llm.getType() == 1 ? "流式" : "非流式") + ")";
        this.modelVersion = llm.getUrl() + "\n" + llm.getRequestMethod() + "\n" + llm.getInParam();
    }

    public void addToLogInfo(String title,String logInfo) {

        if(systemPrompt == null){
            systemPrompt = "";
        }else {
            systemPrompt+= "<br/>\n";
        }
        systemPrompt+= title+":<br/>\n"+logInfo;
    }
}
